//
// Created by hjie on 23-7-2.
//

#include "test.h"
#include <iostream>
#include <vector>

void ProducerData(std::vector<int> & data, int number)
{
    srand(time(NULL));
    for (int index = 0; index < number; index++)
    {
        data.push_back(rand()%2);
    }
    printf("%s : ", __func__ );
    for (auto & item : data)
    {
        printf("%d ", item);
    }
    printf("\n");
}

void NewMethod()
{
    // 标记法，
    int number = 10;
    std::vector<int> number_data;
    ProducerData(number_data, number);
    int * array = &(*number_data.begin());

    int last_zero_index = -1;
    int max_number = 0;

    for (int index = 0; index < number; index++)
    {
        if (array[index] == 0)
        {
            last_zero_index = index;
        }
        else
        {
            max_number = max_number > (index - last_zero_index) ? max_number : (index - last_zero_index);
        }
    }
    printf("max count : %d.\n", max_number);
}

void MaxLinkOne()
{
    int number = 10;
    std::vector<int> number_data;
    ProducerData(number_data, number);
    int * array = &(*number_data.begin());
    int max_one = 0;
    for (int index = 0; index < number; index++)
    {
        if (array[index] != 1)
        {
            continue;
        }
        int count = 0;
        for (int i = index; i < number; i++)
        {
            if (array[i] != 1)
            {
                index = i;
                break;
            }
            else
            {
                count++;
            }
        }
        if (count > max_one)
        {
            max_one = count;
        }
    }
    printf("max count : %d.\n", max_one);
}

int main(int argc, const char * argv[])
{
    for (int i = 0; i < 10; i++)
    {
        MaxLinkOne();
        NewMethod();
        printf("\n");
    }
    return 0;
}